package com.example.controller;


import com.example.pojo.Forum;
import com.example.pojo.ForumTopic;
import com.example.pojo.PageBean;
import com.example.service.ForumService;
import com.example.service.ForumTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-08-20
 */
@Controller
@RequestMapping("/forum")
public class ForumController {
    @Autowired
    private ForumService forumService;
    @Autowired
    private ForumTopicService forumTopicService;

    /**
     * 获取论坛列表
     * @param model
     * @return
     */
    @RequestMapping("/list.do")
    public String getList(Model model,Integer currentPage,Integer pageSize) {
        if (currentPage==null) currentPage = 1;
        if (pageSize==null) pageSize = 5;
        PageBean<Forum> pageBean = forumService.getPage(currentPage, pageSize);
        model.addAttribute("list",pageBean.getRecordList());
        model.addAttribute("pageBean", pageBean);

        model.addAttribute("url", "/forum/list.do?");
        return "/jsp/forum/list.jsp";
    }

    /**
     * 展示具体板块
     *
     * @param fid
     * @return
     */
    @RequestMapping("/show.do")
    public String showTopic(Integer fid, Model model,Integer currentPage,Integer pageSize) {
        if (currentPage == null) {
            currentPage = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        //查询forum
        List<Forum> list = forumService.getList(fid);
        Forum forum = list.get(0);
        model.addAttribute("forum", forum);


        //查询pageBean
        PageBean<ForumTopic> pageBean = forumTopicService.getByPage(currentPage, pageSize, fid);
        model.addAttribute("pageBean", pageBean);
        //查询topicList
        model.addAttribute("topicList", pageBean.getRecordList());

        model.addAttribute("url", "/forum/show.do?fid=" + fid);

        return "/jsp/forum/showTopic.jsp";
    }

}

